<template>
  <div class="w-100 h-100">
    <el-table
      v-loading="loading"
      :data="tableData"
      row-key="messages"
      @row-click="rowClick"
      @selection-change="handleSelectionChange"
      border
      ref="instantiationTable"
      style="width: 100%;"
    >
      <el-table-column type="index" label="序号" width="50" align="center"/>
      <el-table-column prop="dataValue" label="数据值" align="center"/>
      <el-table-column prop="dataValueName" label="数据值名称" align="center"/>
      <el-table-column prop="tagName" label="区域标签" align="center"/>
      <el-table-column prop="deviceName" label="设备名称" align="center"/>
      <el-table-column prop="parameterName" label="元素标签" align="center"/>
      <el-table-column label="数据类型" align="center">
        <template slot-scope="{row}">
          {{row.dataType=='0'?'实时数据':'聚合数据'}}
        </template>
      </el-table-column>
      <el-table-column v-if="selectionOrRadio=='1'" type="selection" reserve-selection width="55"
                       align="center"></el-table-column>
      <el-table-column v-else type="radio" width="55" label="选择" align="center">
        <template slot-scope="scope">
          <el-radio v-model="selectedRowId" :label="scope.row.messages" @change="handleRadioChange(scope.row)">
            {{' '}}
          </el-radio>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      class="pagination"
      :total="count"
      :page.sync="pageIndex"
      :limit.sync="pageSize"
      @pagination="getAccountData"
    />
  </div>
</template>

<script>
  import {
    queryRedisConfigData
  } from "@/api/dataManage/IOTData"

  export default {
    name: "instantiateTable",
    props: {
      selectionOrRadio: {
        default: ""
      }
    },
    data() {
      return {
        projectCode: "",

        searchModel: {
          userName: "",
          status: null //正常/停用状态（全部）
        },

        tableData: [],
        count: 0,
        pageIndex: 1,
        pageSize: 5,
        loading: false,

        selectedRowId: '',
        selectedRow: null, // 用于存储当前选中的行
        selectedRows: [],
      }
    },
    created() {
      this.projectCode = sessionStorage.getItem('projectCode')
      this.getAccountData()
    },
    methods: {
      /**
       * 获取表格方法
       */
      async getAccountData(tableInfo) {
        this.loading = true
        if (tableInfo) {
          const {userName, status} = tableInfo
          const {object} = await queryRedisConfigData({
            projectCode: this.projectCode,
            pageIndex: this.pageIndex,
            pageSize: this.pageSize,
            userName,
            status
          })
          this.tableData = object.data
          this.count = object.count
          this.loading = false
        } else {
          const {object} = await queryRedisConfigData({
            projectCode: this.projectCode,
            pageIndex: this.pageIndex,
            pageSize: this.pageSize
          })
          this.tableData = object.data
          this.count = object.count
          this.loading = false
        }
      },

      rowClick(row, column, event) {
        if (this.selectionOrRadio == '1') {
          // 检查当前行是否已选中
          const index = this.selectedRows.findIndex(item => item.messages === row.messages);

          if (index > -1) {
            // 如果已选中，则取消选中
            this.selectedRows.splice(index, 1);
          } else {
            // 如果未选中，则添加到选中列表
            this.selectedRows.push(row);
          }

          // 更新表格的选择状态
          this.$refs.instantiationTable.toggleRowSelection(row);
        } else {
          this.selectedRowId = row.messages
          this.selectedRow = row
        }
      },
      handleSelectionChange(val) {
        this.selectedRows = val
      },

      handleRadioChange(row) {
        this.selectedRow = row
      },
    }
  }
</script>

<style scoped>
  .pagination {
    display: flex;
    justify-content: flex-end;
  }
</style>
